$(function () {
    console.log("get detail in ");
    getProductDetal(GetUrl("pid"));
    
    $("#addToChart").click(function(){
        //添加到购物车。
        $.ajax({
            url:"/yangcangclub/v1/trolley/add?access_token="+GetUrl("access_token")+"&pid="+GetUrl("pid")+"&num=1",
            type:'GET',
            dataType:"json",
            success:function(data){
                if(data.code == 1000){
                    //成功后跳转到购物车页
                    var productDetailPageUrl=basePath()+"res/gwc.html?access_token="+GetUrl("access_token");
                    window.location.href=productDetailPageUrl;
                    
                }else{
                    layer.open({content: data.msg, skin: 'msg', time: 2});
                }
            },
            error:function(data){
                layer.open({content: '请求失败，请刷新再试', skin: 'msg', time: 2});
            }
        });
        
    });
    
    
});

//ajax获取商品详情
function getProductDetal(id){
    $.ajax({
        url:"/yangcangclub/v1/merchandise/detail?access_token="+GetUrl("access_token")+"&pid="+id,
        type:'GET',
        dataType:"json",
        success:function(data){
            if(data.code == 1000){
                data=data.data;
                $("#bannerUrl").attr("src",data.bannerUrl);
                $("#productName").html(data.name);
                document.title=data.name;
                $("#productPrice").html("￥"+data.unitPrice+'.00');
                if(data.showUnitPrice>0){
                    $("#showPrice").html("价格："+data.showUnitPrice+'.00');
                }
                $(".c-cont").html(data.content);
            }else{
                layer.open({content: data.msg, skin: 'msg', time: 2});
            }
        },
        error:function(data){
            layer.open({content: '请求失败，请刷新再试', skin: 'msg', time: 2});
        }
    });
    
}


